<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div class="" id="app">
        <cpn1></cpn1>
        <cpn1></cpn1>
        <cpn2></cpn2>
    </div>
    <template id="tem1">
        <div id="cpn">
            demo组件1
            <h2 @click="bindClick">{{num}}</h2>
        </div>
    </template>
    <template id="tem2">
        <div id="cpn">
            demo组件2
        </div>
    </template>
    <script>
        const obj={
            num:1
        }
        // 全局组件，模板id必须挂在template上
        // cpn1是注册的组件名，tem1是模板名
        Vue.component('cpn1',{
            template:"#tem1",
            data() {
                return obj
            },
            methods: {
                bindClick(){
                    this.num++;
                }
            },
        })
        new Vue({
            el:"#app",
            data:{
                msg:"main"
            },
            // 局部组件
            components: {
                cpn2:{
                    template:"#tem2"
                },
            },
        })
    </script>
</body>
</html>